import { defineStore } from 'pinia';
import service from '/@/utils/request';
import { query } from '/@/utils/params';
import { easyDate } from '/@/utils/formatTime';

export interface DeviceVo {
	id: string;
	code: string; //
	equipmentmode: string;
	companyId: any;
	dealerId: any;
	status: any;
}
export interface DeviceDTO {
	id: string;
	code: string; //
	equipmentmode: string;
	company: any;
	status: any;
	createdAt: string;
	dealer: any;
}
export const useDevice = defineStore('device', {
	state: () => ({
		devices: [],
	}),
	actions: {
		async getDeviceList(code: string, status: string, dealerId: string, pageSize: number, pageNumer: number) {
			return service.get(query('/api/device', { code, pageSize, pageNumer, status, dealerId })).then((res) => {
				res.list = res.data.list.map((c: DeviceDTO) => {
					return {
						...c,
						createdAt: easyDate(c.createdAt),
						dealerName: c.dealer ? c.dealer.name : '',
						dealerId: c.dealer ? c.dealer.id : '',
					};
				});
				return res;
			});
		},
		async saveDevice(vo: DeviceVo) {
			return service.post('/api/device/create', vo);
		},
		async unboundDevice(id: string) {
			return service.get(`/api/device/unbind/${id}`);
		},
		/**
		 * 编辑
		 */
		async updateDevice(vo: DeviceVo) {
			return service.patch(`/api/device/${vo.id}`, vo);
		},

		async deleteDevice(id: string) {
			return service.delete(`/api/device/${id}`);
		},
		async getModeList(orgId: string) {
			return service.get(`/api/equipmentmode?orgId=${orgId}`).then((res: any) => {
				return res.data.list;
			});
		},
		async getDealerList() {
			return service.get('/api/dealer/downlist').then((res: any) => {
				return res.data.list;
			});
		},
	},
});
